package com.myebills.ebpsfrontend.cust.maxis.ms05;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.myebills.ebpsfrontend.cust.utils.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.User;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.persist.Statement;
import com.checkfree.isolutions.statement.StatementSection;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CusTelSummServlet_05_v4r00 extends EbppxTransactionServlet {

    /**
     *
     *
     */
    public void doTransaction(HttpServletRequest req, HttpServletResponse res)
    throws ServletException {
        try {
            final HttpSession session = req.getSession(false);
            if (null == session) {
                throw new ServletException("Session must be already established");
            }

    		final String randomKey = (String) session.getAttribute(EbppxConstants.RANDOM_KEY);

            final EbppxUrlManager urlManager = getUrlManager(req);

            final String stmtIndex = urlManager.getSecureParameter("stmtIndex");
            if (stmtIndex.length() == 0) {
                throw new ServletException("Missing statement index");
            }

            final String sOidStatement = urlManager.getSecureParameter(EbppxConstants.STATEMENT_ID);
            if (sOidStatement.length() == 0) {
                throw new ServletException("Missing statement identifier");
            }

            final String sOidAccount   = urlManager.getSecureParameter(EbppxConstants.ACCOUNT_ID);
            if (sOidAccount.length() == 0) {
                throw new ServletException("Missing account identifier");
            }

            final String phoneNo = urlManager.getSecureParameter("phoneNo");
            if (phoneNo.length() == 0) {
                throw new ServletException("Missing phone number");
            }

            final String phoneIndex = urlManager.getSecureParameter("phoneIndex");
            if (phoneIndex.length() == 0) {
                throw new ServletException("Missing phone index");
            }

            final String simNo = urlManager.getSecureParameter("simNo");

            final String fromPage = (String) session.getAttribute("fromPage");

            final Account cfiAccount =  getAccountHome().findById(new ObjectId(EncryptionHelper.decrypt(sOidAccount,randomKey)));
            final Statement cfiStatement = getStatementHome().findById(new ObjectId(EncryptionHelper.decrypt(sOidStatement,randomKey)));
            final StatementSection cfiRootSection = cfiStatement.getRootSection();

            // update first view
            cfiStatement.view();

            //update last view
            Date date = new Date();
            cfiStatement.setProperty("dateLastViewed",date.toString()); //EEE MMM dd hh:mm:ss z yyyy

            // usage tag list
            ArrayList usageTagList = (ArrayList)session.getAttribute("usageTagList");
            if (usageTagList != null){
                int size = usageTagList.size();
                for (int i=0; i<size; i++){
                    String key = "usage" + Integer.toString(i);
                    String tagValue =  urlManager.getSecureParameter(key);
                    if (tagValue != null && !tagValue.equals("")){
                        usageTagList.remove(i);
                        usageTagList.add(i,new Integer(tagValue.trim()));
                        session.setAttribute("usageTagList",usageTagList);
                        break;
                    }
                }
            }

            // selected section name
            String selectedSectionName = urlManager.getSecureParameter("selectedSectionName");
            if (selectedSectionName.length() > 0){
                session.setAttribute("selectedSectionName",selectedSectionName);
            }

            req.setAttribute(EbppxConstants.CFI_ACCOUNT, cfiAccount);
            req.setAttribute(EbppxConstants.CFI_STATEMENT, cfiStatement);
            req.setAttribute("rootSection", cfiRootSection);
            req.setAttribute("statementSection", cfiRootSection.getSection("DetailsOfChargesSection"));
            req.setAttribute("stmtIndex", EncryptionHelper.decrypt(stmtIndex,randomKey));
            req.setAttribute("phoneNo", phoneNo);
            req.setAttribute("phoneIndex", phoneIndex);
            req.setAttribute("simNo", simNo);
            req.setAttribute("fromPage",fromPage);

            session.setAttribute("searchVal1","");
            session.setAttribute("searchVal2","");
            session.setAttribute("searchBy","");
            session.setAttribute("grouped","0");

            // log activity start
            if(fromPage.equals("0")){
                final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);
                EbppxCusDBUtil.logUserActivity(EbppxConstants.CUS_VIEW, cfiAccount.getExternalKey(),ecsUser.getUserId(),EbppxConstants.CUSTOMER, EbppxConstants.STATUS_VIEWED, DateUtil.formatDate(new Date()),DateUtil.formatDate(cfiStatement.getDate()),EbppxConstants.FROM_TELSUMM,"","","");
            }
            // log activity end

            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        } catch (ServletException e) {
            // Just re-throw these
            throw e;
        } catch (Throwable t) {
            throw new ServletException(t.toString(), t);
        }
    }
}
